package com.kingstar.ceres.data.remote;

import com.kingstar.ceres.data.remote.sftp.SftpConnection;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ConnectionFactory {

    @Resource
    private ConnectionConfig config;

    private final Map<String,ConnectionWrapper> connectionWrappers=new HashMap<>();

    public ConnectionWrapper fetchConnection(String key){
        return connectionWrappers.get(key);
    }

    public void init(){
        final Map<String, ConnectionConfig.SftpConfig> sftp = config.getSftp();
        registerSftpWrapper(sftp);
        log.info("注册远程连接器结束");
    }

    private void registerSftpWrapper(Map<String, ConnectionConfig.SftpConfig> sftp){
        if(sftp==null || sftp.size()==0){
            return;
        }
        for (Map.Entry<String, ConnectionConfig.SftpConfig> entry : sftp.entrySet()) {
            final SftpConnection connection = createSftpConnectionWrapper();
            connection.setConfig(entry.getValue());
            connectionWrappers.put(entry.getKey(),connection);
        }
    }

    public SftpConnection createSftpConnectionWrapper(){
        return new SftpConnection();
    }
}
